<?php
class inGoodsEdit extends Admin {

    private $tab = array( 'inStock' => '入库管理','inGoodsEdit'=>'货品管理');
    private $color = array('深红', '粉红','橘红','橘黄 ',
                             '玫瑰红','橙色','深黄','淡黄','草绿',
                            '墨绿', '柠檬黄', '黄绿', '蓝绿', '浅绿',
                           '天蓝', '湖蓝', '黑色', '白色', '紫色',
                           '红灰', '黄灰', '蓝灰', '紫灰', '浅灰');
    private $size = array('080CM','085CM','090CM','095CM','100CM',"105CM","110CM",
                            "115CM","120CM","125CM","130CM","135CM","140CM","145CM","150CM","155CM","160CM");

    public function __construct() {
        parent::__construct();
        $this->pk = "inOutStockNo";
    }

    public function index( $c, $a ) {

        $type = G( 'type', 'inGoodsEdit' );
       if('inGoodsEdit' != $type)
       {
           $method = "{$a}_{$type}";
           $this->$method( $c, $a, $type );
           return;
       }

        include $this->display( $c);
    }

    private function index_inStock( $c, $a, $type ) {

        header( "location:?c=inStock");
    }




    public function edit( $c, $a ) {
        $type = G( 'type', 'inStock' );
        if ( !$id = intval( G( 'inOutStockNo' ) ) ) {
            $this->error();
            exit;
        }

        $this->table = 'inOutStock'; // 数据库表名
        $result = $this->find( array( 'inOutStockNo' => $id ) ); // 查找数据
        include $this->display( $c ); // 加载模板
    }

    # 删除商品
    public function del( $c ) {
        if ( !$id = intval( G( "id" ) ) ) {
            exit( '非法删除商品' );
        }
        /**STEP1.查询出记录id为$id的出入库记录*/
        $this->table = 'inOutStockRec';
        $res = $this->find(array('id'=>$id));

        if(!$res)
        {
            return;
        }

        /***STEP2: 更新库存信息表**/
        $this->table = 'stockInfo';
        //$this->pk = "id";
        $where = ' goodsNo = ' . $res['goodsNo'] . ' and color = ' . $res['color']. ' and size = ' . $res['size'];
        if ($result = $this->find($where, 'id')) // 查找数据
        {
            $rows['goodsNumber'] = $result['goodsNumber'] -  $res['goodsNumber'];
            $this->update(array('id'=>$id),$rows);
        }

        /***STEP3: 更新出入库信息表**/
        unset($rows);
        $this->table = 'inOutStock';
        $inOutStockNo = $res['inOutStockNo'];
        $result = $this->find(array('inOutStockNo'=>$inOutStockNo));
        if($result)
        {
            $rows['inOutRecList'] = str_replace(G('id'),"",$result['inOutRecList']);
            $this->update(array('inOutStockNo'=>$inOutStockNo),$rows);
        }

        /***STEP4: 删除出入库记录表**/
        $this->table = 'inOutStockRec';
        $this->delete(array('id'=>$id));


    }




    public function search_Goods( $c, $a ) {


        $this->table = 'inOutStockRec'; // 数据库表名


        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "inOutStockNo ={$key} " ) : '';
        }

        $this->pk = 'inOutStockNo';
        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $res = $result[ 'result' ];

        foreach($res as $key =>$val)
        {
            $res[$key]['color'] = $this->color[$val['color']];
            $res[$key]['size'] = $this->size[$val['size']];
        }
        $result['key'] = G( 'key' );

        include $this->display( $c ); // 加载模板


    }

    public function addGoods($c,$a,$args)
    {
        $type = G('type');
        $result =array('inOutStockNo'=>G('inOutStockNo'),'editMode'=>0);

        $this->table = "inOutStock";
        $res = $this->find(array('inOutStockNo'=>G('inOutStockNo')));
        $result['stockNo'] = $res['stockNo'];

        include $this->display($c);
    }

    public function editGoods($c,$a,$args)
    {
        $id = G('id');
        $this->table = 'inOutStockRec'; // 数据库表名

        $result = $this->find(array('id'=>$id));
        $result['inOutStockNo'] = G('inOutStockNo');
        $result['editMode'] = 1;

        $result['size'] = $this->size[$result['size']];
        $result['color'] = $this->color[$result['color']];

        $this->table = "inOutStock";
        $res = $this->find(array('inOutStockNo'=>G('inOutStockNo')));
        $result['supplierName'] = $res['supplierName'];

        include $this->display($c);
    }



    public function updateGoods()
    {
        /*商品
         * 1.更新出入库记录表
         * 2. 更新库存信息表    stockInfo
         *
         * */

        /*STEP1.更新出入库记录表
              */

        $this->table = 'inOutStockRec';
        $id = G('id');
        $rows['goodsNumber'] = $_POST['goodsNumber'];
        $rows['costPrice'] = $_POST['costPrice'];


        $result = $this->find(array('id'=>$id));
        $number = $result['goodsNumber'];

        if ($result)// 查找数据
        {

            $this->update(array('id'=>$id),$rows);

        }
        /*STEP2.更新出入库记录表  */


        unset($rows);
       $this->table = 'stockInfo';
        //$this->pk = "id";
        $where = ' goodsNo = ' . $_POST['goodsNo'] . ' and color = ' . array_search($_POST['color'],$this->color). ' and size = ' . array_search($_POST['size'],$this->size).' stockNo = ' . $_POST['stockNo'];
        if ($result = $this->find($where, 'id')) // 查找数据
        {
            $rows['goodsNumber'] = $result['goodsNumber'] + $number -  $_POST['goodsNumber'];
            $id = $result['id'];
            $this->update(array('id'=>$id),$rows);
        }

        unset($rows);


}

public function addSaveGoods()
{
    /*   0： 入库  ； 1：出库
     *   1. 更新出入库表单，更新出入库记录id列表   inOutStock
     *   2. 出入库记录表添加记录  inoutStockRec
     *   3. 更新库存信息表    stockInfo
     *
     */


    /**STEP1: 更新出入库表inOutStockRec，
     *
     */


    $this->table = 'inOutStockRec'; // 数据库表名

    if(count($_POST['color']) > 1)
    {
        $judgeVal = 1;           /***判断同一款衣服有不同颜色**/
    }
    else
    {
        $judgeVal = 0;         /***判断同一款衣服有不同尺码**/
    }

    if(1 == $judgeVal)
    {
        foreach ( $_POST['color'] as $key => $val ) {

            /**STEP3:出入库记录表添加记录  inoutStockRec
             *
             */
            $this->table = 'inOutStockRec';
            $rows['color'] = $val;
            $rows['size'] =  $_POST['size'][0];
            $rows[ 'inOutStockNo' ] = $_POST['inOutStockNo'];
            $rows['inOutStyle']= 0 ;

            $rows['goodsNo'] = $_POST['goodsNo'];
            $rows['clothNo'] = $_POST['clothNo'];


            print_r($_POST['goodsNo']);

            $rows['goodsNumber'] = $_POST['goodsNumber'];
            $rows['reason'] = $_POST['inStockType'];
            $rows['costPrice'] = $_POST['costPrice'];
            $rows['stockNo'] = $_POST['stockNo'];

            print_r($rows);

            $id = $this->create( $rows );                         // 添加供货商
            $idList[$key] = $id;

            /*
             *STEP4:跟新库存信息表    stockInfo
             *
             */
            $this->table = 'stockInfo';
            //$this->pk = "id";
            $where =  ' goodsNo = '.$rows['goodsNo'].' and color = '.$rows['color'].' and size = '.$rows['size'].' and stockNo = '.$rows['stockNo'];
            if ($result = $this->find($where, 'id' )) // 查找数据
            {
                $sql = "update xbl_stockInfo set goodsNumber = goodsNumber - ".$rows['goodsNumber'].' where '.$where;
                $this->exec($sql);
            }
            else
            {
                $id = $this->create( $rows );
                $msg = $id ? 'success' : 'failed';
            }
            //$this->pk = "inOutStockNo";
        }
    }
    else
    {
        foreach ( $_POST['size'] as $key => $val ) {

            /*
             * STEP3:出入库记录表添加记录  inoutStockRec
             *
             */
            $this->table = 'inOutStockRec';
            $rows['size'] = $val;
            $rows['color'] = $_POST['color'][0];
            $rows[ 'inOutStockNo' ] = $_POST['inOutStockNo'];
            $rows['inOutStyle']= 0 ;
            $rows['goodsNo'] = $_POST['goodsNo'];
            $rows['clothNo'] = $_POST['clothNo'];

            $rows['goodsNumber'] = $_POST['goodsNumber'];
            $rows['reason'] = $_POST['inStockType'];
            $rows['costPrice'] = $_POST['costPrice'];
            $rows['stockNo'] = $_POST['stockNo'];
            print_r($rows);

            $id = $this->create( $rows ) ;
            $idList[$key] = $id;

            /**STEP4:跟新库存信息表    stockInfo
             *
             */

            $this->table = 'stockInfo';
            //$this->pk = "id";
            $where =  ' goodsNo = '.$rows['goodsNo'].' and color = '.$rows['color'].' and size = '.$rows['size'].' and stockNo = '.$rows['stockNo'];;
            if ($result = $this->find($where, 'id' )) // 查找数据
            {

                $sql = "update xbl_stockInfo set goodsNumber = goodsNumber -".$rows['goodsNumber'].' where '.$where;  //补货添加库存
                $this->exec($sql);
            }
            else
            {
                $id = $this->create( $rows );
                $msg = $id ? 'success' : 'failed';
            }


        }
    }


    /**
     * STEP5: 更新商品信息表  bsInfo
     *
     */

    unset($rows);
    $this->table = 'stockInfo';
    $this->pk = 'id';
    $rows['goodsNo'] = htmlspecialchars( trim( $_POST['goodsNo'] ) );
    $rows[ 'clothNo' ] = htmlspecialchars( trim( $_POST['clothNo'] ) );

    $sql = "select distinct color from xbl_stockInfo where goodsNo = ".$_POST['goodsNo'];

    $res = $this->findSql($sql);

    foreach($res as $key=>$val)
    {
        $rows['color'] .= $this->color[$val['color']].",";
    }

    $sql = "select distinct size from xbl_stockInfo where goodsNo = ".$_POST['goodsNo'];
    $res = $this->findSql($sql);
    foreach($res as $key=>$val)
    {
        $rows['size'] .= $this->size[$val['size']].",";
    }


    // print_r($res);
    // print_r($rows);
    /**
     * （1）确定出库记录Id
     */
    $this->table = "bsInfo";
    $this->pk = "id";
    $res = $this->find(null, null, "max(id)");
    $maxid = intval($res['max(id)']) + 1;


    $this->table = 'bsInfo';
    $rows1['goodsNo'] =  $rows['goodsNo'] ;
    $rows1['materialNo'] =  $rows['clothNo'] ;
    $rows1['color'] = $rows['color'];
    $rows1['size'] = $rows['size'];
    $rows1['id'] = $maxid;
    $rows1['supplierName'] = $_POST['supplierName'];

    $this->pk = "id";
    if($_POST['inStockType'] != 1)
    {
        $this->update(array('goodsNo'=>$rows1['goodsNo']),$rows1);
    }
    else
    {
        $rows1['goodsStatus'] = 0;

        if($result = $this->find(array('goodsNo'=>$rows1['goodsNo'])))
        {
            $this->update(array('goodsNo'=>$rows1['goodsNo']),$rows1);
        }
        else
        {
            $this->create($rows1);
        }


        /**
         * STEP6: 更新商品自然属性表  natureAttr
         *
         */

        $this->table = 'natureAttr';
        $this->pk = "goodsNo";
        $rows['goodsNo'] = $rows1['goodsNo'];
        $rows['color'] = $rows1['color'];
        $rows['size']  = $rows1['size'];
        if($result = $this->find(array('goodsNo'=>$rows1['goodsNo'])))
        {
            $this->update(array('goodsNo'=>$rows1['goodsNo']),$rows1);
        }
        else
        {
            $this->create($rows1);
        }


        unset($rows1);

        /**
         * STEP6: 更新商品价格表  priceDescribe
         *
         */
        $this->table = 'priceDescribe';
        $rows1['goodsNo'] =  $_POST['goodsNo'];
        $rows1['costPrice'] = $_POST['costPrice'];

        $this->pk = "goodsNo";
        $id = $this->create( $rows1 );
        unset($rows1);

    }


    /**
     * （2）确定更新内容项
     */
    $this->table = 'inOutStock'; // 数据库表名
    $condition = array('inOutStockNo' => $_POST['inOutStockNo']);
    $this->pk = 'inOutStockNo';
    $result = $this->find($condition);



    /**zouhong-edit**/
    $rows[ 'inOutStockNo' ] = $_POST['inOutStockNo'];
    $rows['inOutRecList']= $result['inOutRecList'].",".implode(",",$idList);;
    // print_r( $rows['inOutRecList']);

    /**
     * （3）更新出入库表
     */
    /**zouhong-edit-edit**/
    $this->table = 'inOutStock'; // 数据库表名
    $msg = $this->update($condition,$rows) ? 'success' : 'failed';





    unset($result);
    unset($res);


    exit( $msg );
}

    public function saveGoods($c,$a,$args) {



        if(0 == $_POST['editMode'])
        {
            $this->addSaveGoods();
        }
        else
        {
            $this->updateGoods();

        }
 

    }


}
